﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using java.util;
using lineage2.commons.lang;
using lineage2.commons.util;

namespace Game.templates.item
{
    public class RecipeTemplate
    {
	    /**
	     * @author Mobius
	     */
	    public class RecipeComponent : Collection
	    {
		    /**
		     * Field _itemId.
		     */
		    private static int _itemId;
		    /**
		     * Field _count.
		     */
		    private static long _count;
		    /**
		     * Field _chance.
		     */
		    private static int _chance;
		
		    /**
		     * Constructor for RecipeComponent.
		     * @param itemId int
		     * @param count long
		     * @param chance int
		     */
		    public  RecipeComponent(int itemId, long count, int chance)
		    {
			    _itemId = itemId;
			    _count = count;
			    _chance = chance;
		    }
		
		    /**
		     * Constructor for RecipeComponent.
		     * @param itemId int
		     * @param count long
		     */
		    public RecipeComponent(int itemId, long count):this(itemId, count, 0)
		    {
			   //;
		    }
		
		    /**
		     * Method getItemId.
		     * @return int
		     */
		    public int getItemId()
		    {
			    return _itemId;
		    }
		
		    /**
		     * Method getCount.
		     * @return long
		     */
		    public long getCount()
		    {
			    return _count;
		    }
		
		    /**
		     * Method getChance.
		     * @return int
		     */
		    public int getChance()
		    {
			    return _chance;
		    }
	    }
	
	    /**
	     * Field _id.
	     */
	    private int _id;
	    /**
	     * Field _level.
	     */
	    private int _level;
	    /**
	     * Field _mpConsume.
	     */
	    private int _mpConsume;
	    /**
	     * Field _successRate.
	     */
	    private int _successRate;
	    /**
	     * Field _itemId.
	     */
	    private int _itemId;
	    /**
	     * Field _isDwarven.
	     */
	    private bool _isDwarven;
	    /**
	     * Field _materials.
	     */
	    private  IEnumerable<Collection> _materials;
	    /**
	     * Field _products.
	     */
        private IEnumerable<Collection> _products;
	    /**
	     * Field _npcFee.
	     */
        private IEnumerable<Collection> _npcFee;
	
	    /**
	     * Constructor for RecipeTemplate.
	     * @param id int
	     * @param level int
	     * @param mpConsume int
	     * @param successRate int
	     * @param itemId int
	     * @param isDwarven boolean
	     */
	    public RecipeTemplate(int id, int level, int mpConsume, int successRate, int itemId, bool isDwarven)
	    {
		    _materials = new[] {new ArrayList()};
		    _products = new[] {new ArrayList()};
		    _npcFee = new[] {new ArrayList()};
		    _id = id;
		    _level = level;
		    _mpConsume = mpConsume;
		    _successRate = successRate;
		    _itemId = itemId;
		    _isDwarven = isDwarven;
	    }
	
	    /**
	     * Method getId.
	     * @return int
	     */
	    public int getId()
	    {
		    return _id;
	    }
	
	    /**
	     * Method getLevel.
	     * @return int
	     */
	    public int getLevel()
	    {
		    return _level;
	    }
	
	    /**
	     * Method getMpConsume.
	     * @return int
	     */
	    public int getMpConsume()
	    {
		    return _mpConsume;
	    }
	
	    /**
	     * Method getSuccessRate.
	     * @return int
	     */
	    public int getSuccessRate()
	    {
		    return _successRate;
	    }
	
	    /**
	     * Method getItemId.
	     * @return int
	     */
	    public int getItemId()
	    {
		    return _itemId;
	    }
	
	    /**
	     * Method isDwarven.
	     * @return boolean
	     */
	    public bool isDwarven()
	    {
		    return _isDwarven;
	    }
	
	    /**
	     * Method addMaterial.
	     * @param material RecipeComponent
	     */
	    public void addMaterial(RecipeComponent material)
	    {
            List<Collection> temp = _materials.ToList();
            temp.Add(material);
            _materials = temp.ToArray();
	    }
	
	    /**
	     * Method getMaterials.
	     * @return RecipeComponent[]
	     */
	    public RecipeComponent[] getMaterials()
	    {
		    return (RecipeComponent[]) _materials.ToArray();
	    }
	
	    /**
	     * Method addProduct.
	     * @param product RecipeComponent
	     */
	    public void addProduct(RecipeComponent product)
	    {
            List<Collection> temp = _products.ToList();
            temp.Add(product);
            _products = temp.ToArray();
	    }
	
	    /**
	     * Method getProducts.
	     * @return RecipeComponent[]
	     */
	    public RecipeComponent[] getProducts()
	    {
		    return (RecipeComponent[]) _products.ToArray();
	    }
	
	    /**
	     * Method getRandomProduct.
	     * @return RecipeComponent
	     */
	    public RecipeComponent getRandomProduct()
	    {
		    int chancesAmount = _products.Cast<RecipeComponent>().Sum(product => product.getChance());
	        if (Rnd.chance(chancesAmount))
		    {
			    RecipeComponent[] successProducts = new RecipeComponent[0];
			    while (successProducts.Length == 0)
			    {
			        successProducts = _products.Cast<RecipeComponent>().Where(product => Rnd.chance(product.getChance())).Aggregate(successProducts, (current, product) => (RecipeComponent[]) ArrayUtils.add(current, product));
			    }
			    return successProducts[Rnd.get(successProducts.Length)];
		    }
		    return null;
	    }
	
	    /**
	     * Method addNpcFee.
	     * @param fee RecipeComponent
	     */
	    public void addNpcFee(RecipeComponent fee)
	    {
		    List<Collection> temp=_npcFee.ToList();
            temp.Add(fee);
	        _npcFee = temp.ToArray();
            
	    }
	
	    /**
	     * Method getNpcFee.
	     * @return RecipeComponent[]
	     */
	    public RecipeComponent[] getNpcFee()
	    {
		    return (RecipeComponent[]) _npcFee.ToArray();
	    }
    }
}
